package file.manager;

import java.io.File;

import android.app.Activity;
import android.os.Environment;
import android.util.Log;

public class SdCardManager {
	private static final String TAG = "SdCardManager";

	public static boolean writeToSD( String directoryName,
			String fileName, String body, boolean overwrite) {
		if (!verifyExternalStorageStateForWrite())
			return false;

		File root = Environment.getExternalStorageDirectory(); //this is for API Level 7 or lower
//		File root = activity.getExternalFilesDir(null);	//for API 8 or higher
		return FileManager.writeToFile( root, directoryName, fileName, body, overwrite );

	}

	public static boolean readFromSD( String directoryName, String fileName, StringBuilder content) 
	{
		if (!verifyExternalStorageState())
			return false;

		 File root = Environment.getExternalStorageDirectory(); //this is for API Level 7 or lower
//		File root = activity.getExternalFilesDir(null);		//for API 8 or higher
		return FileManager.readFromFile(root, directoryName, fileName, content);
	}

	public static boolean verifyExternalStorageStateForWrite() {
		// MEDIA_MOUNTED_READ_ONLY is read only, MEDIA_MOUNTED is read-write;
		// else is not available
		if (!Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			Log.e(TAG, "Sdcard was not mounted or not writable!");
			return false;
		}
		return true;
	}

	public static boolean verifyExternalStorageState() {
		// MEDIA_MOUNTED_READ_ONLY is read only, MEDIA_MOUNTED is read-write;
		// else is not available
		if (!Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)
				&& !Environment.getExternalStorageState().equals(
						Environment.MEDIA_MOUNTED_READ_ONLY)) {
			Log.e(TAG, "Sdcard was not mounted!");
			return false;
		}
		return true;
	}
}
